package com.hoshiicloud.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.purchase.Worksheet;
import com.hoshiicloud.purchase.feign.IdGeneratorService;
import com.hoshiicloud.purchase.mapper.WorksheetMapper;
import com.hoshiicloud.purchase.service.WorksheetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: hoshiicloud
 * @description: 工单服务实现类
 * @author: Mr.wu
 * @create: 2019-07-24 17:40
 **/
@Slf4j
@Service
public class WorksheetServiceImpl extends ServiceImpl<WorksheetMapper, Worksheet> implements WorksheetService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    @Override
    @CacheEvict(cacheNames = "Worksheet", allEntries = true)
    public boolean saveBatch(List<Worksheet> list) {
        return this.saveBatch(list);
    }

    /**
     * @param id
     * @return com.hoshiibuy.purchase.entity.Worksheet
     * @description: 根据id查询系统工单
     */
    @Cacheable(cacheNames = "Worksheet", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "工单-系统工单-查询系统工单 ")
    public Worksheet getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param entity
     * @return boolean
     * @description: 新增系统工单
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Worksheet", allEntries = true)
    @Logable(authName = "工单-系统工单-新增系统工单")
    public boolean save(Worksheet entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改系统工单
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Worksheet", allEntries = true)
    @Logable(authName = "工单-系统工单管理-修改系统工单")
    public boolean updateById(Worksheet entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
     * @param queryWrapper
     * @return IPage
     * @description: 分页查询系统工单
     */
    @Logable(authName = "工单-系统工单管理-分页查询系统工单")
    @Cacheable(cacheNames = "Worksheet", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ',queryWrapper=' + #queryWrapper.toString() + ')')")
    public IPage<Worksheet> page(IPage<Worksheet> page, Wrapper<Worksheet> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Worksheet", allEntries = true)
    @Logable(authName = "工单-系统工单管理-删除系统工单")
    public boolean deleteById(Worksheet entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    @Override
    @Cacheable(cacheNames = "Worksheet", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "工单-系统工单管理-根据id集合查询系统工单")
    public List<Worksheet> getByIds(List<Long> ids,String key) {
        return baseMapper.selectBatchIds(ids);
    }
}
